package com.dqp.cslggroup.UI;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import com.bumptech.glide.Glide;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.bean.Talk;

import java.util.List;

public class TalkAdapter extends BaseAdapter {
    private List<Talk> talk;
    private LayoutInflater inflater;
    private Context context;

    public TalkAdapter(List<Talk> talk, Context context) {
        this.talk = talk;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return talk == null ? 0 : talk.size();
    }

    @Override
    public Object getItem(int position) {
        return talk.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        @SuppressLint({"ViewHolder", "InflateParams"}) View view = inflater.inflate(R.layout.club_item, null);
        Talk talks = (Talk) getItem(position);
        if (talks.getFine() > 100) {
            LinearLayout linearLayout = view.findViewById(R.id.club_hot);
            linearLayout.setVisibility(View.VISIBLE);
        }

        ImageView club_head = view.findViewById(R.id.club_hade);
        club_head.setVisibility(View.GONE);

        ImageView imageView = view.findViewById(R.id.talk_hade);
        imageView.setVisibility(View.VISIBLE);


        TextView name = view.findViewById(R.id.club_name);
        TextView des = view.findViewById(R.id.club_des);
        des.setTextSize(14);

        TextView fine = view.findViewById(R.id.club_fine);
        TextView bad = view.findViewById(R.id.club_bad);
        TextView comment = view.findViewById(R.id.club_comment);

        TextView date = view.findViewById(R.id.club_date);
        date.setVisibility(View.VISIBLE);
        date.setText(talks.getDates());

        fine.setText(talks.getFine() + "");
        bad.setText(talks.getBad() + "");
        comment.setText(talks.getComment() + "");
        des.setText(talks.getContent());
        name.setText(talks.getName());

        if (talks.getImg() != null && talks.getImg().length() > 10) {
            ImageView talk_img = view.findViewById(R.id.talk_img);
            Glide.with(context).load(talks.getImg()).into(talk_img);
            talk_img.setVisibility(View.VISIBLE);
        }

        if (talks.getHead() != null && talks.getHead().length() > 10)
            Glide.with(context).load(talks.getImg()).into(imageView);
        else if (talks.getGender() != null && talks.getGender().equals("男")) {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.strangerm);
            imageView.setBackground(drawable);
        } else if (talks.getGender() != null && talks.getGender().equals("女")) {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.strangerw);
            imageView.setBackground(drawable);
        } else {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.stranger);
            imageView.setBackground(drawable);
        }
        return view;
    }


}
